package com.winter.service;

import com.google.common.base.Strings;

import com.winter.mapper.FinBondMapper;
import com.winter.mapper.FinContentLikeInfoMapper;
import com.winter.mapper.FinCpaperMapper;
import com.winter.mapper.FinDepositReceiptMapper;
import com.winter.mapper.FinManageMapper;
import com.winter.mapper.FinOnlineMapper;
import com.winter.mapper.FinUnderlineMapper;
import com.winter.model.FinContentLikeInfo;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class LikeService {

    @Autowired
    private FinContentLikeInfoMapper finContentLikeInfoMapper;

    /**
     * 线上
     */
    @Autowired
    private FinOnlineMapper finOnlineMapper;
    /**
     * 线下
     */
    @Autowired
    private FinUnderlineMapper finUnderlineMapper;

    /**
     * 理财
     */
    @Autowired
    private FinManageMapper finManageMapper;

    /**
     * 票据
     */
    @Autowired
    private FinCpaperMapper finCpaperMapper;

    /**
     * 债券
     */
    @Autowired
    private FinBondMapper finBondMapper;


    /**
     * 存单
     */
    @Autowired
    private FinDepositReceiptMapper finDepositReceiptMapper;

    public Result insertLike(FinContentLikeInfo finContentLikeInfo) {
        if (Strings.isNullOrEmpty(finContentLikeInfo.getCreateUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        finContentLikeInfo.setReadFlag("0");
        finContentLikeInfo.setUpdatetime(new Date());
        finContentLikeInfo.setCreatetime(new Date());
        finContentLikeInfo.setUpdateUser(finContentLikeInfo.getCreateUser());

        finContentLikeInfoMapper.insertSelective(finContentLikeInfo);
        if (finContentLikeInfo.getRelaType()
                              .equals("1")) {
            // 线上
            if (finContentLikeInfo.getRelaChildType()
                                  .equals("1")) {
                finOnlineMapper.updateCount(Constants.add, Constants.likeType, finContentLikeInfo.getRelaId());
            }
            // 线下
            if (finContentLikeInfo.getRelaChildType()
                                  .equals("2")) {
                finUnderlineMapper.updateCount(Constants.add, Constants.likeType, finContentLikeInfo.getRelaId());
            }
            // 理财
            if (finContentLikeInfo.getRelaChildType()
                                  .equals("3")) {
                finManageMapper.updateCount(Constants.add, Constants.likeType, finContentLikeInfo.getRelaId());
            }
            // 票据
            if (finContentLikeInfo.getRelaChildType()
                                  .equals("4")) {
                finCpaperMapper.updateCount(Constants.add, Constants.likeType, finContentLikeInfo.getRelaId());
            }
            // 债券
            if (finContentLikeInfo.getRelaChildType()
                                  .equals("6")) {
                finBondMapper.updateCount(Constants.add, Constants.likeType, finContentLikeInfo.getRelaId());
            }
         /*   // 债券
            if (finContentLikeInfo.getRelaChildType()
                                  .equals("7")) {
                finBondMapper.updateCount(Constants.add, Constants.likeType, finContentLikeInfo.getRelaId());
            }*/
            // 存单
            if (finContentLikeInfo.getRelaChildType()
                                  .equals("7")) {
                finDepositReceiptMapper.updateCount(Constants.add, Constants.likeType, finContentLikeInfo.getRelaId());
            }
        }
        return Result.success();
    }

    public Result deleteLike(String type, String chiType, Long id, String userNo) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        finContentLikeInfoMapper.deleteLike(type, chiType, id, userNo);
        if (type.equals("1")) {
            // 线上
            if (chiType.equals("1")) {
                finOnlineMapper.updateCount(Constants.subtract, Constants.likeType, id);
            }
            // 线下
            if (chiType.equals("2")) {
                finUnderlineMapper.updateCount(Constants.subtract, Constants.likeType, id);
            }
            // 理财
            if (chiType.equals("3")) {
                finManageMapper.updateCount(Constants.subtract, Constants.likeType, id);
            }
            // 票据
            if (chiType.equals("4")) {
                finCpaperMapper.updateCount(Constants.subtract, Constants.likeType, id);
            }
            // 债券
            if (chiType.equals("6")) {
                finBondMapper.updateCount(Constants.subtract, Constants.likeType, id);
            }
            // 存单
            if (chiType.equals("7")) {
                finDepositReceiptMapper.updateCount(Constants.subtract, Constants.likeType, id);

            }

        }
        return Result.success();
    }
}
